var app = angular.module("app", []);
app.factory('testInterceptor', [function() {
	return {
		request: function(config) {
			console.log('in request');
			return config;
		},

		requestError: function(config) {
			console.log('in requestError');
			return config;
		},

		response: function(res) {
			console.log(res);
			return res;
		},

		responseError: function(res) {
			console.log('response Error');
			return res;
		}
	}
}]).config(['$httpProvider', function($httpProvider) {
	$httpProvider.interceptors.push('testInterceptor');
}]);
app.service('testService', ['$http', function($http) {
	this.get = function() {
		//return $http.get('http://test-routes.herokuapp.com/test/hello');
		return $http.get('http://test-routes.herokuapp.com/test/hello').then(function(res) {
			//console.log(res);
			return res.data.message;
		}, function(err) {
			return err;
		})
	};
	this.upperCase = function(data) {
		return $http.post('http://test-routes.herokuapp.com/test/uppercase', data);
	};
}])

app.controller('HttpCtrl', ['$scope', 'testService', function($scope, testService) {
	$scope.message = '';
	$scope.getMessage = function() {
		// testService.get().then(function(res){
		// 	console.log(res);
		// 	$scope.message = res.data.message;
		// }, function(err) {
		// 	console.log(err);
		// });

		testService.get().then(function(message) {
			$scope.message = message;
		}, function(err) {
			console.log(err);
		})
	};
	$scope.postData = function(message) {
		testService.upperCase({
			message: message
		}).success(function(body) {
			console.log(body);
			$scope.sendMessage = body.message;
		}).error(function(err) {
			console.log('err:' + err);
		});
	}
}]);


//AngularJS interceptors offer a convenient way to modify request made by the $http service both before they are sent and after they return
